import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'  // 引入 Vuex store

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'manage',
        component: () => import('../views/Manage.vue'),
        children: [
            {
                path: 'user',
                name: '用户管理',
                component: () => import('../views/system/SysUser')
            },
            {
                path: 'role',
                name: '角色管理',
                component: () => import('../views/system/SysRole')
            },
            {
                path: 'function',
                name: '表单管理1',
                component: () => import('../views/function/FunctionManage')
            },
            {
                path: 'mailManage',
                name: '表单管理2',
                component: () => import('../views/mail/MailManage')
            },
            {
                path: 'notifications',
                name: '消息通知',
                component: () => import('../components/SkyNotifications')
            },
            {
                path: 'pageIndex',
                name: '对话',
                component: () => import('../views/system/PageIndex')
            },
            {
                path: 'login',
                name: '登录',
                component: () => import('../views/system/Login')
            },
            {
                path: 'depart',
                name: '部门管理',
                component: () => import('../views/system/SysDepart')
            },
            {
                path: 'drawingCanvas',
                name: '画图',
                component: () => import('../views/drawingCanvas/DrawingCanvas')
            },
            {
                path: 'designer',
                name: '设计',
                component: () => import('../views/function/components/Designer')
            }
        ]
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

// 使用 store 直接访问 Vuex
router.beforeEach((to, from, next) => {
    localStorage.setItem('currentPathName', to.name)
    store.commit('setPath')  // 使用导入的 store，而不是 this.$store
    next()  // 别忘记调用 next() 以继续路由跳转
})

export default router
